﻿using ShoppingOnline.App_Code;
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace ShoppingOnline
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 页面加载时执行的操作
        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserReg.aspx");
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtPassword.Text))
            {
                // 判断用户名或密码是否为空
                Response.Write("<script>alert('用户名或密码不能为空！')</script>");
                return;
            }

            string checksql = "select * from tb_user where userName=@username and userpassWord=@password";
            SqlParameter[] paras = {
                new SqlParameter("@username", txtName.Text),
                new SqlParameter("@password",txtPassword.Text)
            };
            // 执行数据库查询
            DataTable dt = SqlHelper.ExecDataSet(checksql, paras).Tables[0];

            if (dt.Rows.Count > 0)
            {
                // 登录成功，将用户名和用户ID存储在Session中
                Session["usname"] = dt.Rows[0]["username"].ToString();
                Session["userid"] = dt.Rows[0]["userid"].ToString();

                // 弹出登录成功提示框
                Response.Write("<script>alert('登录成功')</script>");

                // 跳转到主页Index.aspx
                Response.Redirect("Index.aspx");
            }
            else
            {
                // 账户或密码错误，弹出提示框
                Response.Write("<script>alert('账户或密码错误')</script>");
                return;
            }
        }
    }
}